// plugins/axios.js
import { defineNuxtPlugin } from '#app'
import axios from 'axios'

export default defineNuxtPlugin(nuxtApp => {
    const config = useRuntimeConfig()
    axios.defaults.baseURL = config.public.baseURL;

    axios.interceptors.request.use(
        config => {
            // 可以在这里添加请求头等
            config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`;
            return config;
        },
        error => {
            return Promise.reject(error);
        }
    );

    axios.interceptors.response.use(
        response => {
            return response;
        },
        error => {
            // 处理错误响应
            return Promise.reject(error);
        }
    );

    nuxtApp.provide('axios', axios); // 使axios可用在整个应用中
});